<?php
/**
 * This class represents the Viewes available in the container. 
 * 
 * @see GadgetsViews::getSupportedViews()
 * @package    gadgets
 * @subpackage feature
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */
class GadgetsSupportedViews implements Instanciatable
{
  /** @var string */
  private $var;
  
  /** @var boolean */
  private $initialized = false;
  
  /** @var string */
  private $eachViewAs;
  
  public function __construct($var)
  {
    $this->var = $var."_array";
  }
  
  public function getVarName()
  {
  	return $this->var;
  }
	
  public function initialize($initialize=true,$appendable=false)
  {
  	$html = '';
  	
    if($initialize)
  	  $html .= 'var '. $this->var." = ";
  	$html .= "gadgets.views.getSupportedViews()";
  	$this->initialized = true;
  	return StringUtil::appendableString($html,$appendable);
  }

  public function initialized()
  {
  	return $this->initialized;
  }
  
  /**
   * This method is used in a foreach inside the view
   *
   * @param string $eachViewAs The name of each variable used inside the foreach
   */
  public function eachAs($eachViewAs)
  {
  	if(!$this->initialized())
  	  throw new sfException('Must initialize firts!');
  	$this->eachViewAs = $eachViewAs;
  	echo "var ".$eachViewAs."_viewType in ".$this->var;
  }	
  
  /**
   * Enter description here...
   *
   * @param boolean $initialize
   * @param boolean $appendable
   * @return GadgetsView The view
   */
  public function getView($echo=true)
  {
  	$view = new GadgetsView(new JSVariable($this->eachViewAs,$this->var.'['.$this->eachViewAs.'_viewType];'));
  	if($echo)
  	  echo $view->getVariable();
    return $view;
  }
}